package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.converter.MsgSourceConverter;
import com.aliyun.messagemgt.dto.MsgAdd;
import com.aliyun.messagemgt.dto.MsgSource;
import com.aliyun.messagemgt.dto.MsgSourceDto;
import com.aliyun.messagemgt.dto.PushChannelDto;
import com.aliyun.messagemgt.dto.PushChannelList;
import com.aliyun.messagemgt.dto.query.MsgQuery;
import com.aliyun.messagemgt.dto.vo.MsgSourceVO;
import com.aliyun.messagemgt.domain.repository.MsgSourceRepository;
import com.aliyun.messagemgt.entities.*;
import com.aliyun.messagemgt.repository.mappers.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author hzw
 * @Date 2020/12/16 16:31
 * @Version 1.0
 */
@Service
public class MsgSourceMybatisRepository implements MsgSourceRepository {
    private static Logger logger = LoggerFactory.getLogger(MsgSourceMybatisRepository.class);
    @Autowired
    private MsgSourceMapper msgSourceMapper;

    @Autowired
    private MsgSourceChannelMapper msgSourceChannelMapper;

    @Autowired
    private PushChannelMapper pushChannelMapper;

    @Autowired
    private BaseReceiveGroupMapper baseReceiveGroupMapper;

    @Autowired
    private BaseMessageSourceMapper baseMessageSourceMapper;

    @Autowired
    private MessageTemplateMapper messageTemplateMapper;

    @Override
    public int countMsgSouece(MsgQuery msgQuery) {
        return msgSourceMapper.countMsgSouece(msgQuery);
    }

    /**
     * 注册发布消息源
     *
     * @param msgSource
     * @return
     */
    @Override
    public Integer addMsgSource(MsgSource msgSource) {
        MsgSourceEntity msgSourceEntity = new MsgSourceEntity();
        BeanUtils.copyProperties(msgSource, msgSourceEntity);
        logger.info("addMsgSource:{}", msgSourceEntity.toString());
        Integer integer = msgSourceMapper.addMsgSource(msgSourceEntity);
        msgSource.setMsgSourceId(msgSourceEntity.getMsgSourceId());
        return integer;
    }

    /**
     * 更新发布消息源
     *
     * @param msgSource
     * @return
     */
    @Override
    public Integer modMsgSource(MsgSource msgSource) {
        MsgSourceEntity msgSourceEntity = new MsgSourceEntity();
        BeanUtils.copyProperties(msgSource, msgSourceEntity);
        logger.info("modMsgSource:{}", msgSourceEntity.toString());
        return msgSourceMapper.modMsgSource(msgSourceEntity);
    }

    /**
     * 查询消息源列表
     *
     * @param msgQuery
     * @return
     */
    @Override
    public Page<MsgSourceVO> list(MsgQuery msgQuery) {
        Page<MsgSourceVO> pageList = msgSourceMapper.list(msgQuery);
        if (pageList.size() < 1){
            return pageList;
        }
        for (MsgSourceVO msgSourceVO : pageList) {
            setPushChannel(msgSourceVO);
        }
        return pageList;
    }

    /**
     * 查询消息源详细信息
     *
     * @param msgQuery
     * @return
     */
    @Override
    public MsgSourceVO queryMsgSourceDetail(MsgQuery msgQuery) {
        MsgSourceVO msgSourceVO = msgSourceMapper.queryMsgSourceDetail(msgQuery);
        setPushChannel(msgSourceVO);
        if (msgSourceVO.getReceiveGroupCode() != null && !msgSourceVO.getReceiveGroupCode().equals("")) {
            QueryWrapper<BaseReceiveGroup> wrapper = new QueryWrapper<>();
            wrapper.eq("receive_group_code", msgSourceVO.getReceiveGroupCode());
            BaseReceiveGroup baseReceiveGroup = baseReceiveGroupMapper.selectOne(wrapper);
            if (baseReceiveGroup != null) {
                msgSourceVO.setReceiveGroupName(baseReceiveGroup.getReceiveGroupName());
            }
        }
        if(msgSourceVO.getSceneCode() != null){
            QueryWrapper<BaseMessageSourceEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("scene_code",msgSourceVO.getSceneCode());
            BaseMessageSourceEntity baseMessageSourceEntity = baseMessageSourceMapper.selectOne(queryWrapper);
            msgSourceVO.setHaveChannel(baseMessageSourceEntity.getHaveChannel());
            msgSourceVO.setHaveGroup(baseMessageSourceEntity.getHaveGroup());
            msgSourceVO.setHaveTemplate(baseMessageSourceEntity.getHaveTemplate());
        }
        if (msgSourceVO.getTemplateId() != null){
            MessageTemplateEntity messageTemplateEntity = messageTemplateMapper.queryByTemplateId(msgSourceVO.getTemplateId());
            msgSourceVO.setTemplateContent(messageTemplateEntity.getTemplateContent());
            msgSourceVO.setTemplateTitle(messageTemplateEntity.getTemplateTitle());
        }
        return msgSourceVO;
    }

    private void setPushChannel(MsgSourceVO msgSourceVO) {
        QueryWrapper<MsgSourceChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("msg_source_id", msgSourceVO.getMsgSourceId());
        queryWrapper.eq("is_deleted", "N");
        List<PushChannelDto> channelDtoList = new ArrayList<>();
        List<MsgSourceChannel> list = msgSourceChannelMapper.selectList(queryWrapper);
        for (MsgSourceChannel msgSourceChannel : list) {
            PushChannelEntity pushChannelEntity = pushChannelMapper.queryByChannelId(msgSourceChannel.getPushChannelId());
            PushChannelDto pushChannelDto = new PushChannelDto();
            BeanUtils.copyProperties(pushChannelEntity, pushChannelDto);
            channelDtoList.add(pushChannelDto);
        }
        msgSourceVO.setPushChannelList(channelDtoList);
    }

    /**
     * 查询分发消息列表
     *
     * @param msgQuery
     * @return
     */
    @Override
    public List<MsgSourceVO> queryMsgList(MsgQuery msgQuery) {
        List<MsgSourceVO> msgSourceVOS = msgSourceMapper.queryMsgList(msgQuery);
        for (MsgSourceVO msgSourceVO : msgSourceVOS) {
            setPushChannel(msgSourceVO);
        }
        return msgSourceVOS;
    }

    /**
     * 根据消息源名字查询消息源个数
     *
     * @param sourceName
     * @return
     */
    @Override
    public int selectCountByName(String sourceName) {
        return msgSourceMapper.selectCountByName(sourceName);
    }

    /**
     * 根据消息源名字查询消息源个数除了自身
     *
     * @param msgAdd
     * @return
     */
    @Override
    public int selectCountByNameExceptSelf(MsgAdd msgAdd) {
        MsgSourceEntity msgSourceEntity = new MsgSourceEntity();
        BeanUtils.copyProperties(msgAdd, msgSourceEntity);
        return msgSourceMapper.selectCountByNameExceptSelf(msgSourceEntity);
    }

    /**
     * 删除消息源
     *
     * @param msgSourceId
     * @return
     */
    @Override
    public int delMsgSource(Long msgSourceId) {
        return msgSourceMapper.delMsgSource(msgSourceId);
    }

    /**
     * 查询渠道状态
     *
     * @param pushChannelId
     * @return
     */
    @Override
    public int queryChannelState(Long pushChannelId) {
        return msgSourceMapper.queryChannelState(pushChannelId);
    }

    /**
     * 根据渠道名称查询一个消息源ID
     *
     * @param description
     * @return
     */
    @Override
    public String selectMsgSourceByChannelName(String description) {
        return msgSourceMapper.selectMsgSourceByChannelName(description);
    }

    /**
     * 查询没有初始化短信配额的短信消息源
     *
     * @return list
     */
    @Override
    public List<MsgSource> queryUnboundSmsQuotaMsgSource() {
        List<MsgSourceEntity> msgSourceEntityList = msgSourceMapper.queryUnboundSmsQuotaMsgSource();
        List<MsgSource> msgSourceList = MsgSourceConverter.INSTANCE.msgSourceEntityList2msgSourceList(msgSourceEntityList);
        return msgSourceList;
    }

    /**
     * 根据编码查询发送任务
     *
     * @param msgSourceId
     * @return
     */
    @Override
    public MsgSource queryBySourceId(String msgSourceId) {
        MsgSourceEntity msgSourceEntity = msgSourceMapper.queryBySourceCode(msgSourceId);
        MsgSource msgSource = MsgSourceConverter.INSTANCE.msgSourceEntity2msgSource(msgSourceEntity);
        return msgSource;
    }

    @Override
    public MsgSource getMsgSourceBySourceCode(String sourceCode) {
        MsgSourceEntity msgSourceEntity = msgSourceMapper.getMsgSourceBySourceCode(sourceCode);
        MsgSource msgSource = MsgSourceConverter.INSTANCE.msgSourceEntity2msgSource(msgSourceEntity);
        return msgSource;
    }

    @Override
    public List<MsgSource> selectMsgSourceByPushChannelId(Integer pushChannelId) {
        List<MsgSourceEntity> msgSourceEntityList = msgSourceMapper.selectMsgSourceByPushChannelId(pushChannelId);
        List<MsgSource> msgSourceList = MsgSourceConverter.INSTANCE.msgSourceEntityList2msgSourceList(msgSourceEntityList);
        return msgSourceList;
    }

    @Override
    public Integer updateStatus(MsgSourceDto msgSourceDto) {
        return msgSourceMapper.updateStatus(msgSourceDto);
    }

    @Override
    public MsgSource getMsgSource(MsgQuery qry) {
        MsgSourceEntity msgSourceEntity = msgSourceMapper.getMsgSource(qry);
        return MsgSourceConverter.INSTANCE.msgSourceEntity2msgSource(msgSourceEntity);
    }

    @Override
    public Integer queryByCode(MsgAdd add) {
        return msgSourceMapper.queryByCode(add);
    }
}
